﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SchoolManagerMVC.BusinessAbstract.Interfaces;
using SchoolManagerMVC.Repository.EntityFramework;
using SchoolManagerMVC.Web.ViewModel;
using SchoolManagerMVC.Web.Models;

namespace SchoolManagerMVC.Web.Areas.Departments.Controllers
{
    [Authorize(Roles = Constants.OfficeAdmin + "," + Constants.Admin)]
    public class HomeController : Controller
    {
        /// <summary>
        /// Department Area
        /// </summary>
        private IRepository _repo;
        public HomeController(IRepository repo)
        {
            _repo = repo;
            if (_repo == null)
                throw new NullReferenceException();
        }
        
        public ActionResult Index()
        {    
            var result = _repo.GetAll<Department>().Select(x => new DepartmentViewModel()
            {
                department = x,
                DepartmentHeadName = _repo.GetWhere<FacultyMember>(e=>e.UniqueID == x.DepartmentHead).Single().FirstLast
            });
            return View("Index",result);
        }

        
        public ActionResult Create()
        {
            ViewBag.DepartmentHeadHolder = _repo.GetAll<FacultyMember>();
            return View("Create");
        }

        [HttpPost]
        public ActionResult Create(Department department)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.DepartmentHeadHolder = _repo.GetAll<FacultyMember>();
                return View("Create", department);
            }
            
            var message = _repo.Add<Department>(department);

            if(message.result)
                return RedirectToRoute("Departments");

            if (message.Message == "Input was not unique")
                ModelState.AddModelError("", "Name must be unique");
            else
                ViewBag.Message = message.Message;

            ViewBag.DepartmentHeadHolder = _repo.GetAll<FacultyMember>();
            return View("Create", department);
        }
        
        public ActionResult Details(int? id)
        {
            if (id == null)
                return RedirectToRoute("Departments");

            var result = _repo.GetWhere<Department>(x => x.DepartmentID == id).Select(e => new DepartmentViewModel()
            {
                department = e,
                DepartmentHeadName = _repo.GetWhere<FacultyMember>(l=>l.UniqueID == e.DepartmentHead).FirstOrDefault().FirstLast
            }).FirstOrDefault();

            return View(result);
        }
        
        public ActionResult Delete(int? id)
        {
            if (id == null)
                return RedirectToRoute("Departments");
            
            var result = _repo.GetWhere<Department>(x => x.DepartmentID == id).Select(e => new DepartmentViewModel()
            {
                department = e,
                DepartmentHeadName = _repo.GetWhere<FacultyMember>(l => l.UniqueID == e.DepartmentHead).FirstOrDefault().FirstLast
            }).FirstOrDefault();

            return View(result);    
        }

        [HttpPost]
        public ActionResult Delete(DepartmentViewModel departmentVM)
        {
            if (departmentVM.department.DepartmentID < 1)
                return RedirectToRoute("Departments");

            var message = _repo.Remove<Department>((_repo.GetSingle<Department>(e => e.DepartmentID == departmentVM.department.DepartmentID) as Department));

            if (message.result)
                return RedirectToRoute("Departments");

            ModelState.AddModelError("",message.Message);
            return View("Delete", departmentVM);
        }

        public ActionResult Edit(int? id)
        {
            if (id == null)
                return RedirectToRoute("Departments");

            var result = (Department)_repo.GetSingle<Department>(e => e.DepartmentID == id);
            ViewBag.DepartmentHeadHolder = _repo.GetAll<FacultyMember>();

            if (result == null)
                return RedirectToRoute("Departments");

            return View("Edit", result);            
        }

        [HttpPost]
        public ActionResult Edit(Department department)
        {
            ViewBag.DepartmentHeadHolder = _repo.GetAll<FacultyMember>();
            if (!ModelState.IsValid)
                return View("Edit", department);

            var result = _repo.GetSingle<Department>(e => e.DepartmentID == department.DepartmentID) as Department;
            result.Name = department.Name;
            result.DepartmentHead = department.DepartmentHead;
            _repo.Update<Department>(result);
            ViewBag.Message = "Record Saved";

            return RedirectToAction("Details", new { id = department.DepartmentID });
        }


        /*        
        [HttpPost]
        public ActionResult MultiDelete(int[] selectedItems)
        {
            if (selectedItems!=null)
            {
                foreach (var curr in selectedItems)
                {
                    _repo.Remove<Department>(_repo.GetSingle<Department>(e => e.DepartmentID == curr) as Department);
                }
            }

            return RedirectToAction("Index");
        }
         * */
    }
}
